const express = require("express");
const getSendData = require("../utils/getSendData");
const multer = require("multer");
const path = require("path");

const router = express.Router();

const storage = multer.diskStorage({
    destination(req, file, callback) {
        callback(null, path.resolve(__dirname, "../public/static/uploads"));
    },
    filename(req, file, callback) {
        const timestamp = Date.now();
        const randomStr = Math.random().toString().slice(-6);
        const ext = path.extname(file.originalname);
        callback(null, `${timestamp}-${randomStr}${ext}`);
    }
});

const upload = multer({
    storage
});

router.post("/", upload.single("file"), async (req, res, next) => {
    res.send(getSendData("/static/uploads/" + req.file.filename));
});

module.exports = router;
